home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / if.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  3.2 KB  |  161 lines

  1. #include <stdio.h>
  2. #include "fudgit.h"
  3. #include "head.h"
  4.  
  5. extern int Ft_Debug;
  6. extern int Ft_almost(register char *str1, register char *str2);
  7.  
  8. static int Ifst[MAXIF];
  9. static int iffi = 0;
  10.  
  11. int Ft_pushif(int val)
  12. {
  13.     Ifst[iffi] = val;
  14.     if (++iffi >= MAXIF) {
  15.         fputs("Error: 'if' too deeply nested.\n", stderr);
  16.         return(ERRR);
  17.     }
  18.     if (Ft_Debug & DEBUG_IF)
  19.         fprintf(stderr, "Pushing value %d in if level %d.\n", val, iffi);
  20.     return(0);
  21. }
  22.  
  23. int Ft_popif(void)
  24. {
  25.     if (--iffi < 0) {
  26.         fputs("Error: Unmatched 'endif'.\n", stderr);
  27.         return(ERRR);
  28.     }
  29.     if (Ft_Debug & DEBUG_IF)
  30.         fprintf(stderr,
  31.         "Popping if level %d (value %d).\n", iffi+1, Ifst[iffi]);
  32.     return(0);
  33. }
  34.  
  35. int Ft_switchif(int val)
  36. {
  37.     if (iffi == 0) {
  38.         fputs("Error: Unmatched 'else'.\n", stderr);
  39.         return(ERRR);
  40.     }
  41.     if (Ft_Debug & DEBUG_IF)
  42.         fprintf(stderr, "Switching level %d if from %d to %d.\n",
  43.         iffi, Ifst[iffi-1], val);
  44.     switch (Ifst[iffi-1]) {
  45.     case DONE_IF:
  46.         if (val == FORCED_IF)
  47.             Ifst[iffi-1] = NFORCE_IF;
  48.         break;
  49.     case FALSE_IF:
  50.         if (val == TRUE_IF)
  51.             Ifst[iffi-1] = TRUE_IF;
  52.         if (val == FORCED_IF)
  53.             Ifst[iffi-1] = FORCED_IF;
  54.         break;
  55.     case TRUE_IF:
  56.         if (val == FORCED_IF)
  57.             Ifst[iffi-1] = NFORCE_IF;
  58.         else
  59.             Ifst[iffi-1] = DONE_IF;
  60.         break;
  61.     case NFORCE_IF:
  62.     case FORCED_IF:
  63.         fputs("else: Non-terminal 'else' in 'if' construction.\n", stderr);
  64.         return(ERRR);
  65.     case DUMMY_IF:
  66.         fputs("else: Bad construction.\n", stderr);
  67.         return(ERRR);
  68.     default:
  69.         fputs("else: Impossible case.\n", stderr);
  70.         return(ERRR);
  71.     }
  72.     return(0);
  73. }
  74.  
  75. static int Dcmode = 0;
  76.  
  77. int Ft_clearpop_if(void)
  78. {
  79.     iffi = 0;
  80.     Dcmode = 0;
  81.     return(0);
  82. }
  83.  
  84. int Ft_iflevel(void)
  85. {
  86.     return(iffi);
  87. }
  88.  
  89. int Ft_ifrun(int c, char *v)
  90. {
  91.  
  92.     if (iffi == 0)
  93.         return(TRUE_IF);
  94.     /***
  95.     fprintf(stderr, "iffi: %d | Dcmode: %d | argc: %d | argv[0]: '%s' \n",
  96.     iffi, Dcmode, c, v);
  97.     ***/
  98.     if (Ifst[iffi-1] != TRUE_IF &&
  99.         Ifst[iffi-1] != FORCED_IF) { /* looking for endif or else */
  100.         if (Ft_almost(v, "cm!ode") && c == 1) {
  101.             if (Dcmode) {
  102.                 fputs("cmode: Double call to cmode.\n", stderr);
  103.                 return(ERRR);
  104.             }
  105.             Dcmode = 1;
  106.             return(FALSE_IF);
  107.         }
  108.         else if (Ft_almost(v, "fm!ode")) {
  109.             if (!Dcmode) {
  110.                 fputs("Warning: Double call to fmode.\n", stderr);
  111.             }
  112.             Dcmode = 0;
  113.             return(FALSE_IF);
  114.         }
  115.         else if (!Dcmode && Ft_almost(v, "if")) {
  116.             if (Ft_pushif(DUMMY_IF) == ERRR) /* push a dummy */
  117.                 return(ERRR);
  118.         }
  119.         else if (Ft_almost(v, "en!dif")) {
  120.             if (Dcmode) {
  121.                 fputs("Error: 'endif' reached while still in cmode.\n",
  122.                 stderr);
  123.                 return(ERRR);
  124.             }
  125.             if (Ft_popif() == ERRR)  /* pop something */
  126.                 return(ERRR);
  127.         }
  128.         else if (Ft_almost(v, "el!se") && Ifst[iffi-1] != DUMMY_IF) {
  129.             if (Dcmode) {
  130.                 fputs("Error: 'else' reached while still in cmode.\n",
  131.                 stderr);
  132.                 return(ERRR);
  133.             }
  134.             return(TRUE_IF);
  135.         }
  136.         return(FALSE_IF);
  137.     }
  138.     return(TRUE_IF);
  139. }
  140.  
  141. int Ft_ifexp(char *cline)
  142. {
  143.     if (!iffi)
  144.         return(YES);
  145.     switch(Ifst[iffi-1]) {
  146.         case FALSE_IF:  /* Waiting for an else or endif */
  147.             if (Ft_almost(cline, "el!se "))
  148.                 return(YES);
  149.         case DUMMY_IF:   /* somewhere nested in a false if */
  150.         case NFORCE_IF:  /* Waiting for a endif */
  151.         case DONE_IF:  /* Waiting for a endif */
  152.             return(NO);
  153.         case TRUE_IF:  /* For sure!  */
  154.         case FORCED_IF:
  155.             return(YES);
  156.         default:
  157.             fputs("Impossible case in ifexp().\n", stderr);
  158.             return(0);
  159.     }
  160. }
  161.